package com.ray.scan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUtil {
    
    public static String get(String url){
        String result = "";
        //用HttpClient发送请求，分为五步
        //第一步：创建HttpClient对象
        HttpClient httpClient = new DefaultHttpClient();
        //第二步：创建代表请求的对象,参数是访问的服务器地址
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            //第三步：执行请求，获取服务器发还的相应对象
            if(response.getStatusLine().getStatusCode() == 200){
                //第五步：从相应对象当中取出数据，放到entity当中
                HttpEntity entity = response.getEntity();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(entity.getContent()));
                result = reader.readLine();
            }else{
                result = String.valueOf(response.getStatusLine().getStatusCode());  
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * post请求
     * @param url
     * @param pairs
     * @return
     */
    public static String post(String url, ArrayList<NameValuePair> pairs){
        String result = "";
        //创建代表请求体的对象（注意，是请求体）
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        //执行请求对象
        try {
            //第三步：执行请求对象，获取服务器发还的相应对象
            HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            //第四步：检查相应的状态是否正常：检查状态码的值是200表示正常
            if(response.getStatusLine().getStatusCode() == 200){
                //第五步：从相应对象当中取出数据，放到entity当中
                HttpEntity entity = response.getEntity();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(entity.getContent()));
                result = reader.readLine();
            }else{
                result = String.valueOf(response.getStatusLine().getStatusCode());
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
}
